package com.expense.annotation;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Map;

import org.apache.log4j.Logger;

/**
 * 数据反射处理类
 * @author xj
 * **/
public class InvokClass {
	public Logger logger = Logger.getLogger(this.getClass());
	/**
	 * 为对象方法赋值
	 * @param  object 对象
	 * @param  fieldName 字段名称
	 * @param  fieldClass 字段类型
	 * @param  value 值
	 * **/
	public  void setValueinvokMethod(String fieldName,Class fieldClass,Object value,Object object) {
			  Method method=null;
			  try {
				  method=object.getClass().getMethod("set"+fieldName.substring(0, 1).toUpperCase()+fieldName.substring(1), fieldClass);
				  method.invoke(object, value);
			  } catch (SecurityException e) {
			   // TODO Auto-generated catch block
			   e.printStackTrace();
			  } catch (NoSuchMethodException e) {
			   // TODO Auto-generated catch block
			   e.printStackTrace();
			  } catch (IllegalArgumentException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}  
	 }
	
	
	/**
	 * 取出对象指定字段的值
	 * @param object 对象
	 * @param fieldName 字段名称
	 * @return 对象中该字段的值
	 * 
	 * **/
	public  Object getValueinvokMethod(String fieldName,Object object) {
			  Object objValue=null;
			  Method method=null;
			  try {
				  method=object.getClass().getMethod("get"+fieldName.substring(0, 1).toUpperCase()+fieldName.substring(1), null);
				  objValue=method.invoke(object, null);
			  } catch (SecurityException e) {
			   // TODO Auto-generated catch block
			   e.printStackTrace();
			  } catch (NoSuchMethodException e) {
			   // TODO Auto-generated catch block
			   e.printStackTrace();
			  } catch (IllegalArgumentException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}  
			return objValue;
	 }
	 
	 /**
	  * 根据类名得到对象
	  * @param className 类名
	  * @return 类名的实例对象
	  * */
	 public  Object classNamtToObj(String className) {
			  Class model=null;
			//获得Model类的实例 
		      Object object=null;
			  try {
			   model = Class.forName(className);
			   object = model.newInstance();
			  } catch (ClassNotFoundException e1) {
			   // TODO Auto-generated catch block
			   e1.printStackTrace();
			  } catch (InstantiationException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} 
	    
			return object;
	 }
	 
	 
   /**
	 * 为对象设置值
	 *  @param object 实例对象
	 *  @param valueMap 列名和值 的map集合
	 *  @return 赋值后的实例对象
	 * **/ 
	public  Object setClassObjInfo(Object object,Map<String, Object> valueMap){
			
			Field[] fields=object.getClass().getDeclaredFields(); 
		    for(int i =0;i<fields.length;i++){
		       Field field = fields[i];
		       String fieldName= field.getName(); //属性名称
		       Class fieldClass = field.getType(); //获取属性字段类型
		       String fieldTypeName=fieldClass.getName();//类型名称
		       String fieldSimpleTypeName=fieldClass.getSimpleName();//简短类型名称
		       //System.out.println("属性名称:"+  fieldName+" 的类型:"+fieldClass);
		       String columnName="";
		       //判断该成员变量上是不是存在Column类型的注解   
	           if(field.isAnnotationPresent(Column.class)){   
	              //存在   
	              Column column = field.getAnnotation(Column.class);//获取实例   
	               //获取元素值   
	               columnName = column.columnName(); 
	           }
	           //这里是里设置属性的值 
		      Method method;
			  try {
				
				method = object.getClass().getMethod("set"+fieldName.substring(0, 1).toUpperCase()+fieldName.substring(1),fieldClass);
	 
				Object value=null;
				if(valueMap.containsKey(columnName)){
					value=valueMap.get(columnName);
				}
				
				//logger.info(columnName+"---"+value);
				
				method.invoke(object,value);
			} catch (SecurityException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				logger.error("err字段设置:"+fieldName);
			} catch (NoSuchMethodException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				logger.error("err字段设置:"+fieldName);
			} catch (IllegalArgumentException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				logger.error("err字段设置:"+fieldName);
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				logger.error("err字段设置:"+fieldName);
			} catch (InvocationTargetException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				logger.error("err字段设置:"+fieldName);
			}  
		       

		    }
			return object;
		}
		

	/**
	 * 输出对象的所有字段值
	 * @param object 对象实例
	 * **/
	public  void printClassObjInfo(Object object){
		logger.info("类名:"+object.getClass().getName());
		String tableName="";
		//判断该类上是不是存在Table类型的注解   
        if(object.getClass().isAnnotationPresent(Table.class)){   
           //存在   
        	Table table = (Table) object.getClass().getAnnotation(Table.class);//获取实例   
            //获取元素值   
        	tableName = table.tableName(); 
        	
        	logger.info("表名是:"+tableName);
        }else{
        	logger.info("没有指定表名");
        }  
		
       
        
		
		Field[] fields=object.getClass().getDeclaredFields(); 
	    for(int i =0;i<fields.length;i++){
	       Field field = fields[i];
	       String fieldName= field.getName(); //属性名称
	       Class fieldClass = field.getType(); //获取属性字段类型
	       String fieldTypeName=fieldClass.getName();//类型名称
	       String fieldSimpleTypeName=fieldClass.getSimpleName();//简短类型名称
	       logger.info("属性名称:"+  fieldName);
	       logger.info("属性名称:"+  fieldName+" 的类型:"+fieldSimpleTypeName);
	       
	       String columnName="";
	       //判断该成员变量上是不是存在Column类型的注解   
           if(field.isAnnotationPresent(Column.class)){   
              //存在   
              Column column = field.getAnnotation(Column.class);//获取实例   
               //获取元素值   
               columnName = column.columnName(); 
               
               logger.info("属性字段:"+  fieldName+"列名是"+columnName);
           }else{
        	   
        	   logger.info("属性字段:"+  fieldName+"没有指定列名");
           }  
           
           
		//   这里是里获取属性的值 
           Method method;
		try {
			Object value="";
			logger.info("get"+fieldName.substring(0, 1).toUpperCase()+fieldName.substring(1));
			method =object.getClass().getMethod("get"+fieldName.substring(0, 1).toUpperCase()+fieldName.substring(1), null) ;
			//objClass.getMethod("get"+fieldName.substring(0, 1).toUpperCase()+fieldName.substring(1),null);
			value=method.invoke(object,null);
			logger.info("属性字段:"+  fieldName+"的值是"+value);
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}  
	      
	   }
		
	}
}
